{{ template "page/head_start" .}}
<style>
  body {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
  }

  .login-container {
    max-width: 450px;
    width: 100%;
  }

  .login-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    overflow: hidden;
    animation: slideUp 0.5s ease-out;
  }

  @keyframes slideUp {
    from {
      opacity: 0;
      transform: translateY(30px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  .login-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 40px 30px;
    text-align: center;
  }

  .login-header h1 {
    font-size: 2rem;
    font-weight: 700;
    margin: 0;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
  }

  .login-header p {
    margin: 10px 0 0 0;
    opacity: 0.9;
    font-size: 0.95rem;
  }

  .login-body {
    padding: 40px 30px;
  }

  .form-floating {
    margin-bottom: 20px;
  }

  .form-floating > .form-control {
    border: 2px solid #e0e0e0;
    border-radius: 12px;
    height: 60px;
    padding-left: 50px;
    transition: all 0.3s ease;
  }

  .form-floating > .form-control:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 0.25rem rgba(102, 126, 234, 0.15);
  }

  .form-floating > label {
    padding-left: 50px;
  }

  .input-icon {
    position: absolute;
    left: 18px;
    top: 50%;
    transform: translateY(-50%);
    color: #667eea;
    font-size: 1.2rem;
    z-index: 4;
  }

  .btn-login {
    width: 100%;
    height: 55px;
    border-radius: 12px;
    font-size: 1.1rem;
    font-weight: 600;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    color: white;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
  }

  .btn-login:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
  }

  .btn-login:active {
    transform: translateY(0);
  }

  .btn-login::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.2);
    transition: left 0.5s ease;
  }

  .btn-login:hover::before {
    left: 100%;
  }

  .settings-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 45px;
    height: 45px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.3);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
  }

  .settings-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: rotate(90deg);
  }

  .password-toggle {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    color: #667eea;
    z-index: 4;
    font-size: 1.2rem;
  }

  .spinner-border-sm {
    width: 1.5rem;
    height: 1.5rem;
  }

  @media (max-width: 576px) {
    .login-header h1 {
      font-size: 1.5rem;
    }

    .login-body {
      padding: 30px 20px;
    }

    .form-floating > .form-control {
      height: 55px;
    }

    .btn-login {
      height: 50px;
      font-size: 1rem;
    }
  }

  .fade-in {
    animation: fadeIn 0.5s ease-in;
  }

  @keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  /* Dark mode support */
  @media (prefers-color-scheme: dark) {
    .login-card {
      background: rgba(30, 30, 30, 0.95);
      color: #e0e0e0;
    }

    .form-floating > .form-control {
      background-color: #2a2a2a;
      border-color: #444;
      color: #e0e0e0;
    }

    .form-floating > label {
      color: #999;
    }
  }
</style>
{{ template "page/head_end" .}}

{{ template "page/body_start" .}}
<div class="login-container">
  <div class="login-card">
    <!-- Settings Button -->
    <div class="settings-btn" data-bs-toggle="modal" data-bs-target="#settingsModal">
      <i class="bi bi-gear-fill"></i>
    </div>

    <!-- Header -->
    <div class="login-header">
      <h1><i class="bi bi-shield-lock-fill me-2"></i>{{ i18n "pages.login.title" }}</h1>
      <p>{{ i18n "pages.login.hello" }}</p>
    </div>

    <!-- Body -->
    <div class="login-body">
      <form id="loginForm">
        <!-- Username -->
        <div class="form-floating position-relative">
          <i class="bi bi-person-fill input-icon"></i>
          <input type="text" class="form-control" id="username" name="username"
                 placeholder='{{ i18n "username" }}' autocomplete="username" required autofocus>
          <label for="username">{{ i18n "username" }}</label>
        </div>

        <!-- Password -->
        <div class="form-floating position-relative">
          <i class="bi bi-lock-fill input-icon"></i>
          <input type="password" class="form-control" id="password" name="password"
                 placeholder='{{ i18n "password" }}' autocomplete="current-password" required>
          <label for="password">{{ i18n "password" }}</label>
          <i class="bi bi-eye-fill password-toggle" id="passwordToggle"></i>
        </div>

        <!-- Two Factor Code (hidden by default) -->
        <div class="form-floating position-relative" id="twoFactorGroup" style="display: none;">
          <i class="bi bi-key-fill input-icon"></i>
          <input type="text" class="form-control" id="twoFactorCode" name="twoFactorCode"
                 placeholder='{{ i18n "twoFactorCode" }}' autocomplete="one-time-code">
          <label for="twoFactorCode">{{ i18n "twoFactorCode" }}</label>
        </div>

        <!-- Submit Button -->
        <button type="submit" class="btn btn-login" id="loginBtn">
          <span id="loginBtnText">{{ i18n "login" }}</span>
          <div class="spinner-border spinner-border-sm text-light d-none" id="loginSpinner"></div>
        </button>
      </form>
    </div>
  </div>

  <!-- Footer -->
  <div class="text-center mt-4 text-white">
    <small>Powered by <strong>Yun Panel</strong> v{{ .cur_ver }}</small>
  </div>
</div>

<!-- Settings Modal -->
<div class="modal fade" id="settingsModal" tabindex="-1" aria-labelledby="settingsModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="settingsModalLabel"><i class="bi bi-gear-fill me-2"></i>{{ i18n "menu.settings" }}</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <!-- Language Selection -->
        <div class="mb-3">
          <label for="languageSelect" class="form-label">{{ i18n "pages.settings.language" }}</label>
          <select class="form-select" id="languageSelect">
            <option value="en-US">🇺🇸 English</option>
            <option value="zh-CN">🇨🇳 简体中文</option>
            <option value="fa-IR">🇮🇷 فارسی</option>
            <option value="vi-VN">🇻🇳 Tiếng Việt</option>
          </select>
        </div>

        <!-- Theme Toggle (Future feature) -->
        <div class="form-check form-switch">
          <input class="form-check-input" type="checkbox" id="darkModeSwitch">
          <label class="form-check-label" for="darkModeSwitch">
            <i class="bi bi-moon-stars-fill me-2"></i>Dark Mode (Coming Soon)
          </label>
        </div>
      </div>
    </div>
  </div>
</div>

{{template "page/body_scripts" .}}
<script>
$(document).ready(function() {
  let twoFactorEnabled = false;

  // Check if two-factor is enabled
  function checkTwoFactor() {
    axios.post('/getTwoFactorEnable', {}, { hideLoader: true })
      .then(response => {
        if (response.data.success) {
          twoFactorEnabled = response.data.obj;
          if (twoFactorEnabled) {
            $('#twoFactorGroup').show();
          }
        }
      })
      .catch(error => {
        console.error('Failed to check two-factor status:', error);
      });
  }

  // Password toggle
  $('#passwordToggle').on('click', function() {
    const passwordInput = $('#password');
    const icon = $(this);

    if (passwordInput.attr('type') === 'password') {
      passwordInput.attr('type', 'text');
      icon.removeClass('bi-eye-fill').addClass('bi-eye-slash-fill');
    } else {
      passwordInput.attr('type', 'password');
      icon.removeClass('bi-eye-slash-fill').addClass('bi-eye-fill');
    }
  });

  // Login form submission
  $('#loginForm').on('submit', function(e) {
    e.preventDefault();

    const username = $('#username').val().trim();
    const password = $('#password').val().trim();
    const twoFactorCode = $('#twoFactorCode').val().trim();

    if (!username) {
      showNotification('{{ i18n "pages.login.toasts.emptyUsername" }}', 'warning');
      return;
    }

    if (!password) {
      showNotification('{{ i18n "pages.login.toasts.emptyPassword" }}', 'warning');
      return;
    }

    // Show loading state
    $('#loginBtn').prop('disabled', true);
    $('#loginBtnText').addClass('d-none');
    $('#loginSpinner').removeClass('d-none');

    // Submit login
    axios.post('/login', {
      username: username,
      password: password,
      twoFactorCode: twoFactorCode
    })
    .then(response => {
      if (response.data.success) {
        showNotification('{{ i18n "pages.login.toasts.successLogin" }}', 'success');
        setTimeout(() => {
          window.location.href = basePath + 'panel/';
        }, 500);
      } else {
        // Reset button state
        $('#loginBtn').prop('disabled', false);
        $('#loginBtnText').removeClass('d-none');
        $('#loginSpinner').addClass('d-none');
      }
    })
    .catch(error => {
      // Reset button state
      $('#loginBtn').prop('disabled', false);
      $('#loginBtnText').removeClass('d-none');
      $('#loginSpinner').addClass('d-none');
    });
  });

  // Language change handler (placeholder for now)
  $('#languageSelect').on('change', function() {
    const selectedLang = $(this).val();
    // TODO: Implement language change functionality
    console.log('Language changed to:', selectedLang);
    showNotification('Language change will be available soon', 'info');
  });

  // Initialize
  checkTwoFactor();

  // Add enter key support for form fields
  $('#username, #password, #twoFactorCode').on('keypress', function(e) {
    if (e.which === 13) {
      $('#loginForm').submit();
    }
  });
});
</script>
{{ template "page/body_end" .}}
